package service

import (
	"strconv"
	"errors"
	. "alarmservice/proconfig"
	"github.com/kaixinmao/xinge"
	"time"
)

var androidClient *xinge.Client
var iosClient *xinge.Client

func GetAndroidClient() *xinge.Client {
	if androidClient == nil {
		//初始化引擎
		//获取配置信息 db section
		dbSection, err := Config.GetSection("androidxinge")
		if err != nil {
			panic("db config not set!" + err.Error())
		}

		accessId, accessIdOk := dbSection["accessid"]
		accessKey, accessKeyOk := dbSection["accesskey"]
		secretKey, secretKeyOk := dbSection["secretkey"]

		if !accessIdOk || !accessKeyOk || !secretKeyOk {
			panic("please set accessid,accesskey,secretkey config")
		}

		androidClient = xinge.NewClient(accessId, 300, accessKey, secretKey)
	}

	return androidClient
}

func GetIosClient() *xinge.Client {
		if iosClient == nil {
		//初始化引擎
		//获取配置信息 db section
		dbSection, err := Config.GetSection("iosxinge")
		if err != nil {
			panic("db config not set!" + err.Error())
		}

		accessId, accessIdOk := dbSection["accessid"]
		accessKey, accessKeyOk := dbSection["accesskey"]
		secretKey, secretKeyOk := dbSection["secretkey"]

		if !accessIdOk || !accessKeyOk || !secretKeyOk {
			panic("please set accessid,accesskey,secretkey config")
		}

		iosClient = xinge.NewClient(accessId, 300, accessKey, secretKey)
	}

	return iosClient
}

func FastGetAndroidMessage(title, content string) (*xinge.AndroidMessage, error) {
	if title == "" || content == "" {
		return nil, errors.New("need params")
	}
	
	message := &xinge.AndroidMessage{
		Content: content,
		Title:   title,
	}
	
	return message, nil
}

func FastGetIosMessage(content string) (*xinge.IosMessage, error) {
	if content == "" {
		return nil, errors.New("need params")
	}
	
	
	
	message := &xinge.IosMessage {
		Aps : &xinge.ApsAttr {
			Alert: content,
		},
	}
	
	return message, nil
}

//推送信息到android系统
func PushAndroidMessageByUids(msg *xinge.AndroidMessage, uids []int64) error {
	
	uidsLen := len(uids)
	if uidsLen == 0 {
		return nil
	}
	
	xingeClient := GetAndroidClient()
	
	uidsStr := make([]string, uidsLen)
	
	for _, v := range uids {
		uidsStr = append(uidsStr, strconv.FormatInt(v, 10))
	}
	
	reqPush := &xinge.ReqPush{
		PushType:     xinge.PushType_multi_account,
		UserAccounts: uidsStr,
		MessageType:  xinge.MessageType_notify,
		Message:      msg,
		ExpireTime:   3000,
		SendTime:     time.Now(),
		MultiPkgType: xinge.MultiPkg_aid,
		PushEnv:      xinge.PushEnv_android,
		PlatformType: xinge.Platform_android,
		LoopTimes:    2,
		LoopInterval: 7,
		Cli:          xingeClient,
	}
	
	err := reqPush.Push()
	return err
}

func PushIOSMessageByUids(msg *xinge.IosMessage, uids []int64, runMode string) error {
	uidsLen := len(uids)
	if uidsLen == 0 {
		return nil
	}
	
	env := xinge.PushEnv_prod
	if runMode != "prod" {
		env = xinge.PushEnv_dev
	}
	
	xingeClient := GetIosClient()
	
	uidsStr := make([]string, uidsLen)
	
	for _, v := range uids {
		uidsStr = append(uidsStr, strconv.FormatInt(v, 10))
	}
	
	reqPush := &xinge.ReqPush{
		PushType:     xinge.PushType_multi_account,
		UserAccounts: uidsStr,
		MessageType:  xinge.MessageType_notify,
		Message:      msg,
		ExpireTime:   3000,
		SendTime:     time.Now(),
		MultiPkgType: xinge.MultiPkg_aid,
		PushEnv:      env,
		PlatformType: xinge.Platform_ios,
		LoopTimes:    2,
		LoopInterval: 7,
		Cli:          xingeClient,
	}
	
	err := reqPush.Push()
	return err
}
